<?php

class Category extends MainModel implements IECartPosition {

	public static function model($className=__CLASS__){
		return parent::model($className);
	}
	
    function getId(){
        return $this->id;
    }

    function getPrice(){
        return $this->price;
    }

	public function tableName(){
		return '{{category}}';
	}

	public function rules(){
		return array(
			array('permalink','ext.yiiext.components.translit.ETranslitFilter','translitAttribute'=>'title'),
			array('show', 'in', 'range'=>array(0, 1)),
			array('title', 'required'),
			array('parent, created, updated, viewcount', 'numerical', 'integerOnly'=>true),
			array('show', 'length', 'max'=>1),
			array('price_factor, price_add', 'numerical'),
			array('image', 'length', 'max'=>10),
			array('title, permalink, alt_title, alt_keywords', 'length', 'max'=>255),
			array('description, alt_description', 'safe'),
			array('created,updated','default','value'=>new CDbExpression(strtotime('now')),'setOnEmpty'=>false,'on'=>'insert'),
			array('updated','default','value'=>new CDbExpression(strtotime('now')),'setOnEmpty'=>false,'on'=>'update'),
			// The following rule is used by search().
			// Please remove those attributes that should not be searched.
			array('id, parent, show, image, title, permalink, description, alt_title, alt_keywords, alt_description, created, updated, viewcount', 'safe', 'on'=>'search'),
		);
	}

	public function relations(){

		return array(
			'items' => array(self::HAS_MANY, 'Item', 'category'),
			'parent' => array(self::BELONGS_TO, 'Category', 'parent'),
		);
	}

	public function attributeLabels(){
		return array(
			'id' => 'ID',
			'parent' => 'Parent',
			'show' => 'Show',
			'image' => 'Image',
			'title' => 'Title',
			'permalink' => 'Permalink',
			'description' => 'Description',
			'alt_title' => 'Alt Title',
			'alt_keywords' => 'Alt Keywords',
			'alt_description' => 'Alt Description',
			'created' => 'Created',
			'updated' => 'Updated',
			'viewcount' => 'Количество просмотров',
			'price_factor' => 'Множитель цены',
			'price_add' => 'Добавочная наценка',
		);
	}

	public function search(){

		$criteria=new CDbCriteria;

		$criteria->compare('id',$this->id,true);
		$criteria->compare('parent',$this->parent);
		$criteria->compare('show',$this->show,true);
		$criteria->compare('image',$this->image,true);
		$criteria->compare('title',$this->title,true);
		$criteria->compare('permalink',$this->permalink,true);
		$criteria->compare('description',$this->description,true);
		$criteria->compare('alt_title',$this->alt_title,true);
		$criteria->compare('alt_keywords',$this->alt_keywords,true);
		$criteria->compare('alt_description',$this->alt_description,true);
		$criteria->compare('created',$this->created);
		$criteria->compare('updated',$this->updated);
		$criteria->compare('viewcount',$this->viewcount);
		$criteria->compare('price_factor',$this->price_factor);
		$criteria->compare('price_add',$this->price_add);

		return new CActiveDataProvider($this, array(
			'criteria'=>$criteria,
		));
	}

    //именованая группа условий - выбираем только категории 2 уровня
    function onlySecondLevel(){
        $this->getDbCriteria()->mergeWith(array(
            'condition'=>'parent != 0',
      	));
      	return $this;
    }
}